package cn.com.infohold.smartcity.activity.Landinfo;

import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.reflect.TypeToken;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import cn.com.infohold.smartcity.R;
import cn.com.infohold.smartcity.api.HttpEvent;
import cn.com.infohold.smartcity.api.SDK;
import cn.com.infohold.smartcity.api.bean.AppendageBean;
import cn.com.infohold.smartcity.api.bean.LandInfoBean;
import cn.com.infohold.smartcity.api.bean.LandResult;
import cn.com.infohold.smartcity.data.XmlData;
import cn.com.infohold.smartcity.parent.ParentActivity;
import cn.com.infohold.smartcity.utils.DialogUtils;
import cn.com.infohold.smartcity.utils.GsonUtil;
import cn.com.infohold.smartcity.utils.RequestCode;
import cn.com.infohold.smartcity.view.WheelView;
import common.annotation.ViewInject;
import common.dialog.ProgressDialog;

/**
 * Created by Monsoon on 2017/11/8.
 */
public class LandInfoAdd_Activity extends ParentActivity {

    @ViewInject(R.id.ib_back)
    ImageButton ib_back;
    @ViewInject(R.id.et_code)
    EditText et_code;
    @ViewInject(R.id.et_name)
    EditText et_name;
    @ViewInject(R.id.et_contractarea)
    EditText et_contractarea;
    @ViewInject(R.id.et_area)
    EditText et_area;
    @ViewInject(R.id.et_surveyarea)
    EditText et_surveyarea;
    @ViewInject(R.id.tv_purpose)
    TextView tv_purpose;
    @ViewInject(R.id.tv_qualitygrade)
    TextView tv_qualitygrade;
    @ViewInject(R.id.tv_landtype)
    TextView tv_landtype;
    @ViewInject(R.id.tv_farmland)
    TextView tv_farmland;
    @ViewInject(R.id.et_remark)
    TextView et_remark;
    @ViewInject(R.id.et_easttill)
    EditText et_easttill;
    @ViewInject(R.id.et_southtill)
    EditText et_southtill;
    @ViewInject(R.id.et_westtill)
    EditText et_westtill;
    @ViewInject(R.id.et_northtill)
    EditText et_northtill;
    @ViewInject(R.id.et_appendname)
    EditText et_appendname;
    @ViewInject(R.id.et_number)
    EditText et_number;
    @ViewInject(R.id.et_unit)
    EditText et_unit;
    @ViewInject(R.id.et_appendremark)
    EditText et_appendremark;
    @ViewInject(R.id.btn_commit)
    Button btn_commit;

    private String userID;
    private LandInfoBean landInfoBean;
    private AppendageBean appendageBean;
    String[] purposeStr = new String[]{"种植业","畜牧业","渔业","其他"};
    String[] qualityStr = new String[]{"一等地","二等地","三等地","四等地","五等地","六等地","七等地","八等地","九等地","十等地"};
    String[] landTypeStr = new String[]{"水田","水浇地","旱地","果园","茶园","其他园地","有林地","灌木林地","其他林地","天然草地","人工牧草地","其他草地",
                                        "空闲地","设施农用地","田坎","盐碱地","沼泽地","沙地","裸地","其他农业用地"};
    String[] farmLandStr = new String[]{"是","否"};

    @Override
    protected Object inflateView() {
        return R.layout.activity_add_landinfo;
    }

    @Override
    protected void initView(View anchor) {

        userID = XmlData.getInstance().getUserInfo().getId();

        landInfoBean = new LandInfoBean();
        appendageBean = new AppendageBean();
        List<AppendageBean> apList = new ArrayList<>();

        List<String> purposeList = new ArrayList<>();
        Collections.addAll(purposeList,purposeStr);
        List<String> qualityList = new ArrayList<>();
        Collections.addAll(qualityList,qualityStr);
        List<String> landtypeList = new ArrayList<>();
        Collections.addAll(landtypeList,landTypeStr);
        List<String> farmlandList = new ArrayList<>();
        Collections.addAll(farmlandList,farmLandStr);

        tv_purpose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DialogUtils.createNewSelectedDialog(LandInfoAdd_Activity.this, "土地用途", new WheelView.OnWheelViewOkClickListener() {
                    @Override
                    public void onClick(View var1, int selectedIndex, String selectValue, List<Integer> selectIndexs, List<String> selectValues) {
                        if (tv_purpose.getText().toString().equals(purposeList.get(selectedIndex).toString())){
                            return;
                        }
                        String purpose = purposeList.get(selectedIndex).toString();
                        tv_purpose.setText(purpose);
                        landInfoBean.setPurpose(purpose);
                    }
                },purposeList,tv_purpose.getText().toString(),false,false,true);
            }
        });

        tv_qualitygrade.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DialogUtils.createNewSelectedDialog(LandInfoAdd_Activity.this, "质量等级", new WheelView.OnWheelViewOkClickListener() {
                    @Override
                    public void onClick(View var1, int selectedIndex, String selectValue, List<Integer> selectIndexs, List<String> selectValues) {
                        if (tv_qualitygrade.getText().toString().equals(qualityList.get(selectedIndex).toString())){
                            return;
                        }
                        String quality = qualityList.get(selectedIndex).toString();
                        tv_qualitygrade.setText(quality);
                        landInfoBean.setQualitygrade(quality);
                    }
                },qualityList,tv_qualitygrade.getText().toString(),false,false,true);
            }
        });

        tv_landtype.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DialogUtils.createNewSelectedDialog(LandInfoAdd_Activity.this, "土地类型", new WheelView.OnWheelViewOkClickListener() {
                    @Override
                    public void onClick(View var1, int selectedIndex, String selectValue, List<Integer> selectIndexs, List<String> selectValues) {
                        if (tv_landtype.getText().toString().equals(landtypeList.get(selectedIndex).toString())){
                            return;
                        }
                        String landtype = landtypeList.get(selectedIndex).toString();
                        tv_landtype.setText(landtype);
                        landInfoBean.setLandtype(landtype);
                    }
                },landtypeList,tv_landtype.getText().toString(),false,false,true);
            }
        });

        tv_farmland.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DialogUtils.createNewSelectedDialog(LandInfoAdd_Activity.this, "是否为基础农田", new WheelView.OnWheelViewOkClickListener() {
                    @Override
                    public void onClick(View var1, int selectedIndex, String selectValue, List<Integer> selectIndexs, List<String> selectValues) {
                        if (tv_farmland.getText().toString().equals(farmlandList.get(selectedIndex).toString())){
                            return;
                        }
                        String landtype = farmlandList.get(selectedIndex).toString();
                        tv_farmland.setText(landtype);
                    }
                },farmlandList,tv_farmland.getText().toString(),false,false,true);
            }
        });

        btn_commit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                landInfoBean.setCode(et_code.getText().toString());
                landInfoBean.setName(et_name.getText().toString());
                landInfoBean.setContractarea(et_contractarea.getText().toString());
                landInfoBean.setArea(et_area.getText().toString());
                landInfoBean.setSurveyarea(et_surveyarea.getText().toString());
                landInfoBean.setPurpose(tv_purpose.getText().toString());
                landInfoBean.setQualitygrade(tv_qualitygrade.getText().toString());
                landInfoBean.setLandtype(tv_landtype.getText().toString());
                switch (tv_farmland.getText().toString()){
                    case "是":
                        landInfoBean.setFarmland(1);
                        break;
                    case "否":
                        landInfoBean.setFarmland(2);
                        break;
                    default:
                        break;
                }
                landInfoBean.setRemark(et_remark.getText().toString());
                landInfoBean.setEasttill(et_easttill.getText().toString());
                landInfoBean.setWesttill(et_westtill.getText().toString());
                landInfoBean.setSouthtill(et_southtill.getText().toString());
                landInfoBean.setNorthtill(et_northtill.getText().toString());
                appendageBean.setName(et_appendname.getText().toString());
                appendageBean.setNumber(et_number.getText().toString());
                appendageBean.setUnit(et_unit.getText().toString());
                appendageBean.setRemark(et_appendremark.getText().toString());
                apList.add(appendageBean);
                landInfoBean.setAppendageBeanList(apList);

                landInfoBean.setPurpose(tv_purpose.getText().toString());
                landInfoBean.setQualitygrade(tv_qualitygrade.getText().toString());
                landInfoBean.setLandtype(tv_landtype.getText().toString());
                if (tv_farmland.getText().toString().equals("是")){
                    landInfoBean.setFarmland(1);
                }else {
                    landInfoBean.setFarmland(2);
                }
                landInfoBean.setManager(userID);
                landInfoBean.setStatus("1");

                initData(landInfoBean);
            }
        });

    }

    private void initData(LandInfoBean bean) {
        SDK.api().updateLandinfo(bean).postEvent(RequestCode.REQUEST_CODE_ADD_LANDINFO);
        ProgressDialog.show(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void getData(HttpEvent<String> httpEvent) {

        int requestCode = httpEvent.getRequestCode();
        if (requestCode != RequestCode.REQUEST_CODE_ADD_LANDINFO) {
            ProgressDialog.dismiss();
            return;
        }

        if (httpEvent.getState() != HttpEvent.STATE_SUCCESS) {
            ProgressDialog.dismiss();
            return;
        } else {
            switch (requestCode) {
                case RequestCode.REQUEST_CODE_ADD_LANDINFO:
                    ProgressDialog.dismiss();
                    Type type = new TypeToken<LandResult<Map<String,String>>>() {
                    }.getType();
                    LandResult<Map<String,String>> result = GsonUtil.fromJson(httpEvent.getData(), type);
                    Toast.makeText(this, result.getMessage(), Toast.LENGTH_SHORT).show();

                    if (result.getStatus().equals("1")) {
                            finish();
                    }
                    break;
                default:
                    break;
            }
        }
    }

}
